package com.shangxia.lucky.common.util.file;


import java.io.*;
import java.util.*;

/**
 * @author zhouya38158
 * @Package com.shangxia.lucky.common.util.file
 * @date 2021/11/16 9:55
 */
public class WriteTxtUtil {

    public static void random(Integer size, Integer son, Integer length, String fileName, Boolean flag) throws IOException {
        Random random = new Random(System.currentTimeMillis());
        BufferedWriter br = new BufferedWriter(new FileWriter(fileName));
        for (int i = 0; i < length; i++) {
            Set<Integer> set = new HashSet<>();
            while (set.size() < son) {
                set.add(random.nextInt(size) + 1);
            }
            StringBuilder sb = new StringBuilder();
            if (!flag) {
                set.forEach(s -> sb.append(s + " "));
            } else {
                List<Integer> list = new ArrayList<>(set);
                list.sort(Integer::compareTo);
                list.forEach(s -> sb.append(s + " "));
            }
            br.write(sb.toString());
            br.newLine();
        }
        br.close();
    }
}
